package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.WorkOvertimeAuditDTO;
import com.eastfair.home.dto.WorkOvertimeAuditPageQuery;
import com.eastfair.home.dto.WorkOvertimeAuditSaveDTO;
import com.eastfair.home.dto.WorkOvertimeAuditUpdateDTO;
import com.eastfair.home.entity.WorkOvertimeAudit;
import com.eastfair.home.service.WorkOvertimeAuditService;
import com.eastfair.home.vo.WorkOvertimeAuditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 加班审核
 * </p>
 *
 * @author dq
 * @date 2022-10-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/workOvertimeAudit")
@Api(value = "WorkOvertimeAudit", tags = "加班审核")
@PreAuth(replace = "home:workOvertimeAudit:")
public class WorkOvertimeAuditController extends SuperController<WorkOvertimeAuditService, Long, WorkOvertimeAudit, WorkOvertimeAuditPageQuery, WorkOvertimeAuditSaveDTO, WorkOvertimeAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private WorkOvertimeAuditService workOvertimeAuditService;

    @Override
    public void handlerResult(IPage<WorkOvertimeAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<WorkOvertimeAudit> workOvertimeAuditList = list
                .stream()
                .map((map) -> WorkOvertimeAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(workOvertimeAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<WorkOvertimeAuditPageQuery> params) {
        WorkOvertimeAuditPageQuery pageQuery = params.getModel();
    }

    /**
     * 分页查询加班审核列表VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询加班审核列表VO")
    @PostMapping("/query/workOvertimeAudit")
    public R<Page<WorkOvertimeAuditVO>> queryWorkOvertimeAudit(@RequestBody PageParams<WorkOvertimeAuditDTO> pageParams) {
        log.info("queryWorkOvertimeAudit - 分页查询加班审核列表VO, pageParams={}", pageParams);
        return R.successDef(workOvertimeAuditService.listWorkOvertimeAuditVO(pageParams));
    }

    /**
     * 加班单号获取加班审核详情.
     *
     * @param workOvertimeNumber the work overtime number
     * @return the by work overtime number
     */
    @ApiOperation("加班单号获取加班审核详情")
    @GetMapping("/get/{workOvertimeNumber}")
    public R<WorkOvertimeAuditVO> getByWorkOvertimeNumber(@PathVariable("workOvertimeNumber") String workOvertimeNumber) {
        log.info("getByWorkOvertimeNumber - 加班单号获取加班审核详情, workOvertimeNumber={}", workOvertimeNumber);
        return R.successDef(workOvertimeAuditService.getVOByWorkOvertimeNumber(workOvertimeNumber));
    }

    /**
     * 提交加班申请审核.
     *
     * @param workOvertimeAuditDTO the work overtime audit dto
     * @return the r
     */
    @ApiOperation("提交加班申请审核")
    @PostMapping("/submit")
    public R<Boolean> submitWorkOvertimeAudit(@RequestBody WorkOvertimeAuditDTO workOvertimeAuditDTO) {
        log.info("submitWorkOvertimeAudit - 提交加班申请审核, workOvertimeAuditDTO={}", workOvertimeAuditDTO);
        try {
            return R.successDef(workOvertimeAuditService.submitWorkOvertimeAudit(workOvertimeAuditDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 审核加班申请.
     *
     * @param workOvertimeAuditDTO the work overtime audit dto
     * @return the r
     */
    @ApiOperation("审核加班申请")
    @SysLog("审核加班申请")
    @PostMapping("/audit")
    public R<Boolean> auditWorkOvertime(@RequestBody WorkOvertimeAuditDTO workOvertimeAuditDTO) {
        log.info("auditWorkOvertime - 审核加班申请, workOvertimeAuditDTO={}", workOvertimeAuditDTO);
        return R.successDef(workOvertimeAuditService.auditWorkOvertime(workOvertimeAuditDTO));
    }

    /**
     * 取消加班申请审核.
     *
     * @param workOvertimeNumber the work overtime number
     * @return the r
     */
    @ApiOperation("取消加班申请审核")
    @SysLog("取消加班申请")
    @GetMapping("/cancel/{workOvertimeNumber}")
    public R<Boolean> cancelWorkOvertimeAudit(@PathVariable("workOvertimeNumber") String workOvertimeNumber) {
        log.info("cancelWorkOvertimeAudit - 取消加班申请审核, workOvertimeNumber={}", workOvertimeNumber);
        try {
            return R.successDef(workOvertimeAuditService.cancelWorkOvertimeAudit(workOvertimeNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
